package token

import (
	"context"
	"fmt"
)

type Service interface {
	IssueToken(context.Context, *IssueTokenRequest) (*Token, error)
	RevolkToken(context.Context, *RevolkTokenRequest) (*Token, error)
	ValidateToken(context.Context, *ValidateTokenRequest) (*Token, error)
}

type (
	IssueTokenRequest struct {
		Username   string `json:"username" validate:"required"`
		Password   string `json:"password" validate:"required"`
		IsRemember bool   `json:"is_remember"` // 是否记住我
	}
	RevolkTokenRequest struct {
		AccessToken  string `json:"access_token" validate:"required"`
		RefreshToken string `json:"refresh_token" validate:"required"`
	}
)

func NewValidateTokenRequest(ac string) *ValidateTokenRequest {
	return &ValidateTokenRequest{AccessToken: ac}
}

type ValidateTokenRequest struct {
	AccessToken string `json:"access_token" validate:"required"`
}

func (req *IssueTokenRequest) Validate() error {
	if req.Username == "" {
		return fmt.Errorf("username is required")
	}
	if req.Password == "" {
		return fmt.Errorf("password is required")
	}
	return nil
}

func NewIssueTokenRequest(username, password string) *IssueTokenRequest {
	return &IssueTokenRequest{
		Username: username,
		Password: password,
	}
}

func DefaultIssueTokenRequest() *IssueTokenRequest {
	return &IssueTokenRequest{}
}

func (req *RevolkTokenRequest) Validate() error {
	if req.AccessToken == "" {
		return fmt.Errorf("access_token is required")
	}
	if req.RefreshToken == "" {
		return fmt.Errorf("refresh_token is required")
	}
	return nil
}

func NewRevolkTokenRequest(at, rt string) *RevolkTokenRequest {
	return &RevolkTokenRequest{
		AccessToken:  at,
		RefreshToken: rt,
	}
}
